package com.emellend.api.r;

import com.emellend.api.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
/**
 * 关于星星按钮的用法
 * 其中，numStars是星星的总数，rating是当前的评分数
 * @author tiangh
 *
 */
public class RatingBar1 extends Activity implements OnRatingBarChangeListener {

	private TextView mRatingText;
	private RatingBar mIndicatorRatingBar;
	private RatingBar mSmallRatingBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ratingbar_1);
		init();
		addListener();
	}
	
	private void init() {
		mRatingText = (TextView) this.findViewById(R.id.rating);
		mIndicatorRatingBar = (RatingBar) this.findViewById(R.id.indicator_ratingbar);
		mSmallRatingBar = (RatingBar) this.findViewById(R.id.small_ratingbar);
	}
	
	private void addListener() {
		((RatingBar)this.findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
		((RatingBar)this.findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
	}
	
	public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
		final int numStars=ratingBar.getNumStars();
		mRatingText.setText("Rating: "+rating+"/"+numStars);
		
		if (mIndicatorRatingBar.getNumStars()!=numStars) {
			mIndicatorRatingBar.setNumStars(numStars);
			mSmallRatingBar.setNumStars(numStars);
		}
		if (mIndicatorRatingBar.getRating() != rating) {
            mIndicatorRatingBar.setRating(rating);
            mSmallRatingBar.setRating(rating);
        }
		
		final float ratingBarStepSize = ratingBar.getStepSize();
        if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) {
            mIndicatorRatingBar.setStepSize(ratingBarStepSize);
            mSmallRatingBar.setStepSize(ratingBarStepSize);
        }
	}

}
